/**
 * Created by jinzhao on 16-7-29.
 */
$(document).ready(function () {
    $('.edit_button').live('click', function () {
        var e_cname = $(this).parent().parent().children('.cname');
        var cname =  e_cname.text();
        $(e_cname).html('<input type="text" name="cname" class="input" size="15" maxlength="15">');
        $(e_cname).children('.input').val(cname);
        $(this).text('确定');
        $(this).mouseup(function () {
            $(this).addClass('confirm_button');
            $(this).removeClass('edit_button');
        });
    });
    
    
    $('.confirm_button').live('click', function () {
        var e_cname = $(this).parent().parent().children('.cname');
        var cname =  e_cname.children('.input').val();
        var active = this;
        var cid = $(this).parent().parent().children('.cid').text();
        $.post('./admin/cate/update_cate_ajax.html',
            {
                cid:cid,
                cname:cname
            },
            function (data, status) {
                if (status == 'success'){
                    if (data == 'ok') {
                        $(active).mouseup(function () {
                            $(this).addClass('edit_button');
                            $(this).removeClass('confirm_button');
                        });
                        $(active).text('编辑');
                        $(e_cname).html(cname);
                    }else {
                        alert(data);
                    }
                }else {
                    alert(data);
                }
            }
        );
        // if (result == true) {
        //     $(this).mouseup(function () {
        //         $(this).addClass('edit_button');
        //         $(this).removeClass('confirm_button');
        //     });
        //     $(this).text('编辑');
        //     $(e_cname).html(cname);
        // }
    });
    
    
    $('.del').click(function () {
        var ret = confirm('确定要删除吗');
        if (ret == true){
            var item = $(this).parent().parent();
            var e_name = item.children('.cid'); 
            var cid = e_name.text();
            $.post(
                './admin/cate/del_cate.html',
                {
                    cid : cid
                },
                function (data, status) {
                    if (status == 'success'){
                        if (data == 'ok'){
                            item.remove();
                        }else {
                            alert(data);
                        }
                    }else {
                        alert(data);
                    }
                }
            );
        }
    });
});
